#pragma once
#include "Limitor.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Diagnostics;
using namespace System::Security::Permissions; 

namespace BESCLR {

	/// <summary>
	/// Summary for LimitorUI
	/// </summary>
	public ref class LimitorUI : public System::Windows::Forms::UserControl
	{
	private:
		CLimitor^ limitor;
	public:
		LimitorUI(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			limitor = gcnew CLimitor();
			limitor->m_HWMD= (HWND)Handle.ToPointer();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~LimitorUI()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  StartButton;
	private: System::Windows::Forms::Button^  StopButton;
	private: System::Windows::Forms::Label^  StatusLabel;
	private: System::Windows::Forms::Label^  TargetLabel;
	private: System::Windows::Forms::TrackBar^  LimitTrackBar;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->StartButton = (gcnew System::Windows::Forms::Button());
			this->StopButton = (gcnew System::Windows::Forms::Button());
			this->StatusLabel = (gcnew System::Windows::Forms::Label());
			this->TargetLabel = (gcnew System::Windows::Forms::Label());
			this->LimitTrackBar = (gcnew System::Windows::Forms::TrackBar());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->LimitTrackBar))->BeginInit();
			this->SuspendLayout();
			// 
			// StartButton
			// 
			this->StartButton->Location = System::Drawing::Point(371, 5);
			this->StartButton->Name = L"StartButton";
			this->StartButton->Size = System::Drawing::Size(75, 23);
			this->StartButton->TabIndex = 0;
			this->StartButton->Text = L"Start";
			this->StartButton->UseVisualStyleBackColor = true;
			this->StartButton->Click += gcnew System::EventHandler(this, &LimitorUI::StartButton_Click);
			// 
			// StopButton
			// 
			this->StopButton->Location = System::Drawing::Point(290, 5);
			this->StopButton->Name = L"StopButton";
			this->StopButton->Size = System::Drawing::Size(75, 23);
			this->StopButton->TabIndex = 1;
			this->StopButton->Text = L"Stop";
			this->StopButton->UseVisualStyleBackColor = true;
			this->StopButton->Click += gcnew System::EventHandler(this, &LimitorUI::StopButton_Click);
			// 
			// StatusLabel
			// 
			this->StatusLabel->AutoSize = true;
			this->StatusLabel->Font = (gcnew System::Drawing::Font(L"SimSun", 15, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->StatusLabel->ForeColor = System::Drawing::Color::Orange;
			this->StatusLabel->Location = System::Drawing::Point(7, 52);
			this->StatusLabel->Name = L"StatusLabel";
			this->StatusLabel->Size = System::Drawing::Size(86, 20);
			this->StatusLabel->TabIndex = 2;
			this->StatusLabel->Text = L"Calm OK";
			// 
			// TargetLabel
			// 
			this->TargetLabel->AutoSize = true;
			this->TargetLabel->Font = (gcnew System::Drawing::Font(L"SimSun", 13, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->TargetLabel->ForeColor = System::Drawing::Color::Orange;
			this->TargetLabel->Location = System::Drawing::Point(7, 16);
			this->TargetLabel->Name = L"TargetLabel";
			this->TargetLabel->Size = System::Drawing::Size(148, 18);
			this->TargetLabel->TabIndex = 3;
			this->TargetLabel->Text = L"Target missing";
			// 
			// LimitTrackBar
			// 
			this->LimitTrackBar->AutoSize = false;
			this->LimitTrackBar->Location = System::Drawing::Point(254, 32);
			this->LimitTrackBar->Maximum = 100;
			this->LimitTrackBar->Name = L"LimitTrackBar";
			this->LimitTrackBar->Size = System::Drawing::Size(192, 25);
			this->LimitTrackBar->TabIndex = 4;
			this->LimitTrackBar->Value = 67;
			this->LimitTrackBar->ValueChanged += gcnew System::EventHandler(this, &LimitorUI::LimitTrackBar_ValueChanged);
			// 
			// LimitorUI
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::Transparent;
			this->Controls->Add(this->LimitTrackBar);
			this->Controls->Add(this->TargetLabel);
			this->Controls->Add(this->StatusLabel);
			this->Controls->Add(this->StopButton);
			this->Controls->Add(this->StartButton);
			this->DoubleBuffered = true;
			this->Name = L"LimitorUI";
			this->Size = System::Drawing::Size(476, 73);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->LimitTrackBar))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void StartButton_Click(System::Object^  sender, System::EventArgs^  e) {
					 Start();
			 }
    
	public: void SetTarget(int pid)
			{
				limitor->SetTarget(pid);
			}
	public: void Start()
			{
				limitor->Start();

			}
	public: void Stop()
			{
				limitor->Stop();
			}
	private: System::Void StopButton_Click(System::Object^  sender, System::EventArgs^  e) {
				 Stop();
			 }
	private: System::Void LimitTrackBar_ValueChanged(System::Object^  sender, System::EventArgs^  e) {
			 limitor->SetTimeGreen(LimitTrackBar->Value);
		 }
	protected:
	[SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
	virtual void WndProc (Message% m) override
	{
		 switch ( m.Msg )
         {
            case WM_USER_STOP:
				if(m.LParam.ToInt32()==START)
				{
					StatusLabel->Text="Start OK";
				}
				else if(m.LParam.ToInt32()==STOP)
				{
					StatusLabel->Text="Stop OK";
				}
				else if(m.LParam.ToInt32()==TARGET_MISSING)
				{
					StatusLabel->Text="Target missing";
				}
            return;
         }
		UserControl::WndProc(m);
	}
};
}
